import time
import hashlib
from curl_cffi import requests
import codecs


def mcode_algorithm_v1(appid, dev, udid, version):
    """
    基于Hook分析的MCODE算法还原 - 版本1
    假设g0.a是MD5算法的实现
    """
    # 步骤1: 拼接所有参数
    concatenated = appid + dev + udid + version
    print(f"Step 1 - Concatenated: {concatenated}")

    # 步骤2: 转小写并添加固定盐值
    with_salt = concatenated.lower() + "IO2#!spopqd"
    print(f"Step 2 - With salt: {with_salt}")

    # 步骤3: 三重MD5哈希
    hash1 = hashlib.md5(with_salt.encode('utf-8')).hexdigest()
    print(f"Step 3.1 - First MD5: {hash1}")

    hash2 = hashlib.md5(hash1.encode('utf-8')).hexdigest()
    print(f"Step 3.2 - Second MD5: {hash2}")

    hash3 = hashlib.md5(hash2.encode('utf-8')).hexdigest()
    print(f"Step 3.3 - Third MD5: {hash3}")

    # 步骤4: 转大写并取前8位
    result = hash3.upper()[:8]
    print(f"Step 4 - Final result: {result}")

    return result
def md5(str):
    return hashlib.md5(str.encode("utf-8")).hexdigest()
proxies = {
    'http': 'http://127.0.0.1:7890',
    'https': 'http://127.0.0.1:7890'
}
headers = {
    "Host": "search.myzaker.com",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Android WebView\";v=\"138\"",
    "sec-ch-ua-mobile": "?1",
    "sec-ch-ua-platform": "\"Android\"",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Linux; Android 13; 23013RK75C Build/TKQ1.220905.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/138.0.7204.179 Mobile Safari/537.36 ZAKER/9.1.5",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "appinfo": "zaker 9.1.5 androidphone",
    "X-Requested-With": "com.myzaker.ZAKER_Phone",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
}
url = "https://search.myzaker.com/api/"
key = "WvT6dYtChFRdb7yKsearch"
_version="9.08"
_time=str(int(time.time()))
_appid="AndroidPhone"
_udid="1fb67af59b5699a6"
_v="9.1.5"
_dev="39"
def get_mcode(_appid,_dev,_udid,_v):
    _mcode=_appid+_dev+_udid+_v
    return _mcode
def get_flag():
    parr=key+_appid+_udid+_version+_time
    jiami1=md5(parr)
    jiami2=md5(jiami1)
    print("jiami2=====>",jiami2)
    return jiami2

params = {
    "_webcode": "85e5e681d8838e4a37db",
    "_flag": get_flag(),
    "_os": "13_23013RK75C",
    "sign_arg": "search|_appid,_udid,_uid,_version,keyword",
    "app_ids": "660,310000,13,9,4,11542,5,7,3,8,10530,14,11581,14733",
    "_os_name": "23013RK75C",
    "_mcode": "782ECE9C",
    "_only_read": "N",
    "_clicode": "drkjeSEfcYDX1teOXihrCj2XzcFRNfVX8%2BYz4c8F97ou09PFuxwXRz5MiGDDPUNGhem0wpj6%2FrZjDz2db1S7FvanFsXVXzuSqDN0%2F9H5fXQboq5tdRLZNTtTWglt14Ow2up6UUUAOroimS1HlUPRnt%2FCQ4I%2BuQaYbMO3tBgfzppDreyPYJ3gJetESX4Iz9g%2F",
    "appType": "normal",
    "_v": _v,
    "_udid": _udid,
    "_appid": _appid,
    "_dev": _dev,
    "keyword": "马斯克",
    "full_arg": "_udid",
    "_version": _version,
    "_time": _time,
    "_brand": "Redmi"
}
response = requests.get(url, headers=headers, params=params,proxies=proxies)
# 对整个响应文本进行解码
decoded_text = codecs.decode(response.text, 'unicode_escape')
print(decoded_text)

# 写入文件时也解码
with open("search2.html", "w", encoding="utf-8") as f:
    f.write(decoded_text)